import type { CONTAIN_SUB } from '@/enum/modules/public'
import { get, post, request } from '../ajax'
import { getAjaxBaseUrl } from '../handler'

import type {
  MeasuringToolsQueryParams,
  MeasuringToolsSaveOrUpdateParams,
} from '@/types'

const controller = getAjaxBaseUrl(
  '/measuringTools',
  '/energy',
)

/** 查询计量器具分页列表 */
export function selectMeasuringToolsPage(params: Partial<MeasuringToolsQueryParams>) {
  return post<any>(
    `${controller}/selectMeasuringToolsPage`,
    params,
  )
}

/** 新增或修改计量器具信息 */
export function saveOrUpdateMeasuringTools(data: Partial<MeasuringToolsSaveOrUpdateParams>) {
  return post(`${controller}/saveOrUpdateMeasuringTools`, data)
}

/** 查询计量器具信息 */
export function loadMeasuringTools(id: number) {
  return get(`${controller}/loadMeasuringTools`, {
    id,
  })
}

/** 删除计量器具信息 */
export function deleteMeasuringToolsById(id: number) {
  return get(`${controller}/deleteMeasuringToolsById`, {
    id,
  })
}

/** 查询计量器具树 */
export function selectMeasuringToolsTree() {
  return get(`${controller}/selectMeasuringToolsTree`)
}

/** 查询计量器具 */
export function selectMeasuringTools() {
  return get(`${controller}/selectMeasuringTools`)
}

/** 查询计量器具列表 */
export function selectMeasuringToolsList() {
  return get(`${controller}/selectMeasuringToolsList`)
}

/** 获取导入计量器具模板 */
export function getMeasuringToolsTemplate() {
  return request({
    url: `${controller}/template`,
    method: 'GET',
    responseType: 'blob',
  })
}

/** 导入计量器具信息 */
export function importMeasuringTools(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return post(`${controller}/importMeasuringTools`, formData)
}

/** 导出计量器具 */
export function exportMeasuringTools(params: {
  /** 是否包含下级 0 否，1 是  */
  containSub: CONTAIN_SUB
  energyId?: number
  pointId: number
}) {
  return post(`${controller}/exportMeasuringTools`, params)
}

/** 删除计量器具报告 */
export function deleteMeasuringToolsAnnexById(id: number) {
  return get(`${controller}/deleteReport/${id}`)
}

/** 分页查询计量器具报告 */
export function selectMeasuringToolsAnnexPage(toolId: number, pageNumber: number, pageSize: number) {
  return get(`${controller}/measuringToolsReportPage/${toolId}`, {
    pageNumber,
    pageSize,
  })
}

/** 导入计量器具报告 */
export function importMeasuringToolsAnnex(toolId: number, files: File[]) {
  const formData = new FormData()
  files.forEach((file) => {
    formData.append('files', file)
  })

  return request({
    url: `${controller}/uploadReport/${toolId}`,
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

// ----------------------------------------------------
// --------------------电力质量分析仪--------------------
// ----------------------------------------------------
/** 查询卡片预览 */
export async function selectMeasuringToolsCount() {
  return get<{
    /** 站点总数 */
    zdCount: number
    /** 器具数量 */
    toolsCount: number
    /** 测点数量 */
    indicatorCount: number
  }>(`${controller}/selectMeasuringToolsCount`)
}

/** 查询设备运行状态饼图 */
export async function selectMeasuringToolsWorkingCount() {
  return get<Record<string, string>>(`${controller}/selectMeasuringToolsWorkingCount`)
}

/** 查询计量器具下点位趋势图 */
export async function loadIndicatorActualTimeByToolsId(toolsId: number) {
  return get(`${controller}/loadIndicatorActualTimeByToolsId?toolsId=${toolsId}`)
}
/** 查询某用能单元下所有计量器具 */
export async function selectSimpleMeasuringTools() {
  return get<Record<string, string>>(`${controller}/selectSimpleMeasuringTools`)
}

/** 查询某用能单元下所有测点当前值 */
export async function loadIndicatorActualTimePage(pageNumber: number = 1, pageSize: number = 10) {
  return post<{
    total: number
    records: {
      pointName: string
      toolsCode: string
      toolsName: string
      indicatorName: string
      indicatorCode: string
      indicatorNumber: string
      dataTime: string
      val: number
    }[]
  }>(`${controller}/loadIndicatorActualTimePage`, { pageNumber, pageSize })
}

/** 碳排放配置指标管理-分页查计量器具 */
export async function selectPointMeasuringToolsPage(params: {
  pointId: number
  queryText?: string
  pageNumber: number
  pageSize: number
}) {
  return post(`${controller}/selectPointMeasuringToolsPage`, params)
}
